package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.view.HackyViewPager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import photoview.PhotoView;
import photoview.PhotoViewAttacher;

/**
 * Created by Administrator on 2015/11/18.
 */
public class ViewPagerActivity extends BaseActivity {
    private static final String ISLOCKED_ARG = "isLocked";

    private ViewPager mViewPager;

    private List<String> items;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hack_layout);
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
        Bundle bundle = getIntent().getExtras();

        String[] urls = bundle.getStringArray("imgurls");
        items = new ArrayList<>();
        Collections.addAll(items, urls);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(new SamplePagerAdapter());
        mViewPager.setCurrentItem(getIntent().getIntExtra("currentItem", 0));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int curruntIndex = 0;
            public void onPageScrollStateChanged(int arg0) {
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            public void onPageSelected(int index) {
                if (curruntIndex > index){
                    PointRecordManage.getInstance().addPoint(mySelf, Piont.SourcePictureRight);
                }else {
                    PointRecordManage.getInstance().addPoint(mySelf, Piont.SourcePictureLeft);
                }
                curruntIndex = index;
            }

        });
//        if (savedInstanceState != null) {
//            boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
//            ((HackyViewPager) mViewPager).setLocked(isLocked);
//        }
    }

    private class SamplePagerAdapter extends PagerAdapter {



        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            Glide.with(ViewPagerActivity.this).load(items.get(position) + "?imageMogr2/thumbnail/!640x360r").placeholder(R.drawable.car_loading)
                    .into(photoView);


            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    PointRecordManage.getInstance().addPoint(mySelf, Piont.SourcePictureBack);
                    finish();
                }
            });
//            photoView.setImageResource(items[position]);

            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.viewpager_menu, menu);
//        return super.onCreateOptionsMenu(menu);
//    }

//    @Override
//    public boolean onPrepareOptionsMenu(Menu menu) {
//        menuLockItem = menu.findItem(R.id.menu_lock);
//        toggleLockBtnTitle();
//        menuLockItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                toggleViewPagerScrolling();
//                toggleLockBtnTitle();
//                return true;
//            }
//        });
//
//        return super.onPrepareOptionsMenu(menu);
//    }

    private void toggleViewPagerScrolling() {
        if (isViewPagerActive()) {
            ((HackyViewPager) mViewPager).toggleLock();
        }
    }

//    private void toggleLockBtnTitle() {
//        boolean isLocked = false;
//        if (isViewPagerActive()) {
//            isLocked = ((HackyViewPager) mViewPager).isLocked();
//        }
//        String title = (isLocked) ? getString(R.string.menu_unlock) : getString(R.string.menu_lock);
//        if (menuLockItem != null) {
//            menuLockItem.setTitle(title);
//        }
//    }

    private boolean isViewPagerActive() {
        return (mViewPager != null && mViewPager instanceof HackyViewPager);
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        if (isViewPagerActive()) {
            outState.putBoolean(ISLOCKED_ARG, ((HackyViewPager) mViewPager).isLocked());
        }
        super.onSaveInstanceState(outState);
    }

}

